package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class ListController {

    @Autowired
    ListFeignClient listFeignClient;

    @GetMapping("/list")
    public String list(SearchParam searchParam, Model model){
        model.addAttribute("searchParam",searchParam);
        //响应结果
        SearchResponseVo searchResponseVo = listFeignClient.search(searchParam);
        //品牌列表
        model.addAttribute("trademarkList",searchResponseVo.getTrademarkList());
        //拼接请求地址
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //平台属性
        model.addAttribute("attrsList",searchResponseVo.getAttrsList());
        //排序
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        //商品
        model.addAttribute("goodsList",searchResponseVo.getGoodsList());
        //当前页
        model.addAttribute("pageNo",searchResponseVo.getPageNo());
        //总页数
        model.addAttribute("totalPages",searchResponseVo.getTotalPages());

        //选择品牌属性回显
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);

        //选择平台属性回显
        List<SearchAttr> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        return "list/index";
    }

    private List<SearchAttr> buildPropsParamList(SearchParam searchParam) {

        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            List<SearchAttr> searchAttrList = Arrays.stream(props).map(prop -> {
                SearchAttr searchAttr = new SearchAttr();
                String[] split = prop.split(":");
                searchAttr.setAttrId(Long.parseLong(split[0]));
                searchAttr.setAttrValue(split[1]);
                searchAttr.setAttrName(split[2]);
                return searchAttr;
            }).collect(Collectors.toList());
            return searchAttrList;
        }
        return null;
    }

    private String buildTrademarkParam(SearchParam searchParam) {

        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = trademark.split(":");
            return "品牌：" + split[1];
        }
        return null;
    }

    private Map buildOrderMap(SearchParam searchParam) {

        Map map = new HashMap();
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            map.put("type",split[0]);
            map.put("sort",split[1]);
        }else {
            map.put("type","1");
            map.put("sort","asc");
        }
        return map;
    }

    //拼接请求地址
    private String buildUrlParam(SearchParam searchParam) {

        StringBuilder sb = new StringBuilder();

        String keyword = searchParam.getKeyword();
        if (!StringUtils.isEmpty(keyword)){
            sb.append("keyword=").append(keyword);
        }

        //拼接3级ID URL
        Long category1Id = searchParam.getCategory1Id();
        if (category1Id != null){
            sb.append("&category1Id=").append(category1Id);
        }
        Long category2Id = searchParam.getCategory2Id();
        if (category2Id != null){
            sb.append("&category2Id=").append(category2Id);
        }
        Long category3Id = searchParam.getCategory3Id();
        if (category3Id != null){
            sb.append("&category3Id=").append(category3Id);
        }

        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            sb.append("&trademark=").append(trademark);
        }

        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }

        return "/list?" + sb.toString();
    }

}
